package com.adpython.dao.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.springframework.orm.jdo.support.JdoDaoSupport;

import com.adpython.dao.PMF;
import com.adpython.dao.UsersDao;
import com.adpython.domain.Users;

public class UsersDaoImpl extends JdoDaoSupport implements UsersDao {

	public Users get(Long id){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		try{
			Extent r = pm.getExtent (Users.class, false);
			String filter = "id=="+id;
			Query query = pm.newQuery(r,filter);
			List<Users> list = (List<Users>) query.execute();
			if(!list.isEmpty())
				return list.get(0);
			else
				return null;
		}finally{
			pm.close();
		}
	}

	public void remove(Users user){
		if(user!=null)
			this.removeById(user.getId());
	}
	
	public void removeById(Long id){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Transaction tx=pm.currentTransaction();
		try{
			tx.begin();
			Extent r = pm.getExtent (Users.class, false);
			String filter = "id=="+id;
			Query query = pm.newQuery(r,filter);
			List<Users> list = (List<Users>) query.execute();
			if(!list.isEmpty()){
				Users delUser = list.get(0);
				pm.deletePersistent(delUser);
			}
			tx.commit();
		}finally{
			if(tx.isActive())
				tx.rollback();
			pm.close();
		}
	}

	public void save(Users user){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Transaction tx=pm.currentTransaction();
		try{
			tx.begin();
			pm.makePersistent(user);
			tx.commit();
		}finally{
			if(tx.isActive())
				tx.rollback();
			pm.close();
		}
	}

	public void update(Users user){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Users old=pm.getObjectById(Users.class, user.getId());
		Transaction tx=pm.currentTransaction();
		try{
			tx.begin();
			old.setCreateTime(user.getCreateTime());
			old.setIsActive(user.getIsActive());
			old.setIsValid(user.getIsValid());
			old.setMail(user.getMail());
			old.setPassword(user.getPassword());
			old.setUpdateTime(new Date());
			tx.commit();
		}finally{
			if(tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
	
	public List<Users> queryAll(){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		try{
			Query query=pm.newQuery(Users.class);
			query.setOrdering("id asc");
			return (List<Users>) pm.detachCopyAll((Collection<Users>) query.execute());
		}finally{
			pm.close();
		}
	}
	
	public Users queryByMail(String mail){
		PersistenceManager pm=PMF.get().getPersistenceManager();
		try{
			Query query=pm.newQuery(Users.class);
			query.setFilter("mail=='"+mail+"'");
			List<Users> list=(List<Users>) pm.detachCopyAll((Collection<Users>) query.execute());
			if(list.size()>0)
				return list.get(0);
			else
				return null;
		}finally{
			pm.close();
		}
	}
	
}
